package com.nirvana.factory;

import com.nirvana.resolve.*;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class FileResolveFactory {
    private static final Map<String, AbstractResolver> map = new HashMap<>();
    public static AbstractResolver getResolver(File file) {
        String name = file.getName();
        String suffix = name.substring(name.lastIndexOf(".") + 1);
        if (map.containsKey(suffix)) {
            AbstractResolver resolver = map.get(suffix);
            resolver.setFile(file);
            return resolver;
        }
        AbstractResolver resolver;
        switch (suffix) {
            case "xml" -> resolver = new XmlResolver(file);
            case "xlsm", "xlsx" -> resolver = new ExcelResolver(file);
            case "scala" -> resolver = new DefResolver(file);
            // case "fbs" -> resolver = new FbsResolver(file);
            default -> throw new RuntimeException();
        }
        map.put(suffix, resolver);
        return map.get(suffix);
    }
}
